package com.manor.paradise.activity

import android.content.Intent
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.blankj.utilcode.util.ClickUtils
import com.blankj.utilcode.util.SpanUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CenterCrop
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.noober.background.drawable.DrawableCreator
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.ClickUtil
import com.manor.common.utils.ToastUtil
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.bean.ParadiseInfoBean
import com.manor.paradise.http.ElvesHttpConst
import com.manor.paradise.http.ElvesHttpUtil
import com.manor.common.custom.BorderRoundTransformation
import kotlinx.android.synthetic.main.dialog_world_show.*
import java.math.BigDecimal

/**
 * 我的世界
 */

class MyWorldActivity : AbsActivity(), View.OnClickListener {


    override fun getLayoutId(): Int {
        return R.layout.dialog_world_show
    }


    override fun main() {
        super.main()
        im_MyHiredElves.setOnClickListener(this)
        im_back.setOnClickListener(this)
        imAuraIndex.setOnClickListener(this)
        imAuraValue.setOnClickListener(this)
        tv_crystal.setOnClickListener(this)
        tv_gxz.setOnClickListener(this)

        ClickUtils.applyPressedViewScale(im_back, imAuraIndex, imAuraValue, tv_crystal)

        ElvesHttpUtil.getParadiseInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                var infoBean: ParadiseInfoBean = GsonUtil.gsonToBean(info, ParadiseInfoBean::class.java)

                Glide.with(this@MyWorldActivity).load(infoBean.avatar)
                        .apply(RequestOptions()
                                .transform(CenterCrop(),
                                        CircleCrop(),
                                        BorderRoundTransformation(this@MyWorldActivity, im_av.width / 2, 0, 5, Color.WHITE, 4)
                                )
                        )
                        .into(im_av)
                ll_jz.setOnClickListener {
                    JzActivity.forward(this@MyWorldActivity, infoBean)
                }

                val eStar = when {
                    infoBean.npc.compareTo(BigDecimal("1")) == 0 -> "5"
                    infoBean.npc.compareTo(BigDecimal("2")) == 0 -> "6"
                    else -> infoBean.star.toString()
                }


                setLevel(eStar, tvLevel, ivLevel, bl_lay)


                tv_noble.visibility = View.GONE
//                if (infoBean.icon == "") {
//                    tv_noble.visibility = View.GONE
//                } else {
//                    tv_noble.visibility = View.VISIBLE
//                    Glide.with(this@MyWorldActivity)
//                            .load(infoBean.icon)
//                            .into(tv_noble)
//                }

                tv_name.text = infoBean.userNikeName
                tv_id.text = "淘米号：" + infoBean.serialNo

                SpanUtils.with(tv_gxz)
                        .append("贡献值：")
                        .setForegroundColor(Color.parseColor("#FFFFFFFF"))
                        .append(infoBean.contribute.toString())
                        .setForegroundColor(Color.parseColor("#FC7B3B"))
                        .setUnderline()
                        .create()

            }

        })
    }


    override fun onClick(v: View?) {
        if (ClickUtil.canClick(500))
            when (v?.id) {
                R.id.im_MyHiredElves -> {
                    MyEmploymentActivity.forward(this, 0)
                    // this@MyWorldActivity.startActivity(Intent(this@MyWorldActivity, MyEmploymentActivity::class.java))
                }
                R.id.im_back -> {
                    finish()
                }
                R.id.imAuraIndex -> {
                    this@MyWorldActivity.startActivity(Intent(this@MyWorldActivity, ReikiIndexActivity::class.java))
                }
                R.id.imAuraValue -> {
                    this@MyWorldActivity.startActivity(Intent(this@MyWorldActivity, AuraValueActivity::class.java))
                }
                R.id.tv_crystal -> {
                    this@MyWorldActivity.startActivity(Intent(this@MyWorldActivity, CrystalActivity::class.java))
                }
                R.id.tv_gxz -> {
                    ExplainActivity.forward(this, ExplainActivity.CONTRIBUTE_DESCRIPTION)
                }

            }
    }

    private fun setLevel(star: String, tv: TextView, img: ImageView, lay: LinearLayout) {
        //(-1无 0准星 1一星 2二星 3三星)
        when (star) {
            "0" -> {
                tv.text = "一星农场主"
                img.setBackgroundResource(R.mipmap.pic_y_jz0)
                lay.background = getLevelDrawable()
            }
            "1" -> {
                tv.text = "二星农场主"
                img.setBackgroundResource(R.mipmap.pic_y_jz1)
                lay.background = getLevelDrawable()
            }
            "2" -> {
                tv.text = "三星农场主"
                img.setBackgroundResource(R.mipmap.pic_y_jz2)
                lay.background = getLevelDrawable()
            }
            "3" -> {
                tv.text = "四星农场主"
                img.setBackgroundResource(R.mipmap.pic_y_jz3)
                lay.background = getLevelDrawable()
            }
            "5" -> {
                tv.text = "区域合伙人"
                img.setBackgroundResource(R.mipmap.pic_y_jz5)
                lay.background = getLevelDrawable()
            }
            "6" -> {
                tv.text = "城市合伙人"
                img.setBackgroundResource(R.mipmap.pic_y_jz6)
                lay.background = getLevelDrawable()
            }
            else -> {
                tv.text = "一星农场主"
                img.setBackgroundResource(R.mipmap.pic_n_jz1)
                lay.background = DrawableCreator.Builder()
                        .setCornersRadius(DpUtil.dp2px(8).toFloat())
                        .setSolidColor(Color.parseColor("#FF484747"))
                        .build()
            }
        }

    }

    private fun getLevelDrawable(): Drawable =
            DrawableCreator.Builder()
                    .setCornersRadius(DpUtil.dp2px(8).toFloat())
                    .setSolidColor(Color.parseColor("#FF3F89FF"))
                    .build()

    override fun onDestroy() {
        Glide.get(this@MyWorldActivity).clearMemory()
        ElvesHttpUtil.cancel(ElvesHttpConst.POST_OPEN_SHOP_BY_UID)
        super.onDestroy()
    }
}